package cn.finder.estimote;

import java.util.ArrayList;
import java.util.List;

import com.estimote.sdk.Beacon;
import com.estimote.sdk.BeaconManager;
import com.estimote.sdk.Region;
import com.estimote.sdk.Utils;

/**
 * Created by finder on 2015/12/25 0025.
 */
public abstract class FinderRangingListener implements BeaconManager.RangingListener{

	//保存当前发现的beacon列表
	//private List<FinderBeacon> beaconList=new ArrayList<FinderBeacon>();
	
	
    /***
     * all is default 单位为米
     */
    private double radius=99999.0f;

    /***
     * 缓冲距离
     */
    private double bufferDistance=0.0f;

    public FinderRangingListener(){
    }
    
    public FinderRangingListener(double radius, double bufferDistance){
        this.radius=radius;
        this.bufferDistance=bufferDistance;
    }

    public void onBeaconsDiscovered(Region region,List<Beacon> beacons) {


        if(beacons!=null&&beacons.size()>0){

        	//区域距离半径
        	double distance=radius+bufferDistance; 

            List<FinderBeacon> newBeacons=new ArrayList<FinderBeacon>();

            for(int i=0;i<beacons.size();i++){
                Beacon item=beacons.get(i);
                double actDistance= Utils.computeAccuracy(item);
                if(actDistance<=distance){
                	
                	FinderBeacon finderBeacon=new FinderBeacon(item.getProximityUUID(),item.getMacAddress(),item.getMajor(),item.getMinor(),item.getMeasuredPower(),item.getRssi());
                    newBeacons.add(finderBeacon);
                    
                }

            }

            onBeaconsDiscovered(newBeacons,region);

        }

    }

    /***
     * beacons discoverd
     * @param region
     * @param beacons
     */
    public abstract void onBeaconsDiscovered(List<FinderBeacon> beacons,Region region);


    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double getBufferDistance() {
        return bufferDistance;
    }

    public void setBufferDistance(double bufferDistance) {
        this.bufferDistance = bufferDistance;
    }
}
